package com.ruoyi.merchant.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.merchant.other.dto.ConfirmPaymentDto;
import com.ruoyi.merchant.other.dto.CouponCollectionDto;
import com.ruoyi.merchant.other.dto.CouponQrCodeDto;
import com.ruoyi.merchant.service.IMerchantAppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * MerchantAppController
 * 
 * @author dongdong
 * @date 2025-10-21
 */
@Validated
@CrossOrigin
@RestController
@RequestMapping("/app/merchant")
public class MerchantAppController extends BaseController
{
    @Autowired
    private IMerchantAppService merchantAppService;

    /**
     * 查询商家信息列表
     */
    @GetMapping("/login")
    public AjaxResult login(@NotBlank(message = "用户名或密码不能为空") String username,
                            @NotBlank(message = "用户名或密码不能为空") String password) {
        return AjaxResult.success(merchantAppService.login(username, password));
    }

    /**
     * @param search 搜索值（可选）
     * @param merchantId （必填）
     * @return
     * 查询平台优惠券列表
     */
    @GetMapping("/couponList")
    public AjaxResult couponList(String search, @NotNull(message = "请登录") Long merchantId) {
        return AjaxResult.success(merchantAppService.couponList(search, merchantId));
    }

    /**
     * 查询优惠券信息
     */
    @GetMapping("/getCoupon/{id}")
    public AjaxResult getCoupon(@PathVariable("id") Long id) {
        return AjaxResult.success(merchantAppService.getCoupon(id));
    }

    /**
     * 发送优惠券时查询优惠券信息
     */
    @GetMapping("/couponIssueInfo/{orderNo}")
    public AjaxResult couponIssueInfo(@PathVariable("orderNo") String orderNo) {
        return AjaxResult.success(merchantAppService.couponIssueInfo(orderNo));
    }

    /**
     * 购买优惠券支付
     * @return
     */
    @PostMapping("/confirmPayment")
    public AjaxResult confirmPayment(@RequestBody @Validated ConfirmPaymentDto confirmPaymentDto){
        return success(merchantAppService.confirmPayment(confirmPaymentDto));
    }


    /**
     * 查询商家优惠券列表
     */
    @GetMapping("/merchantCouponList")
    public AjaxResult merchantCouponList(Integer status, @NotNull(message = "请登录") Long merchantId) {
        return AjaxResult.success(merchantAppService.merchantCouponList(status, merchantId));
    }

    /**
     * 生成二维码
     */
    @PostMapping("/createQrCode")
    public AjaxResult createQrCode(@RequestBody @Validated CouponQrCodeDto couponQrCodeDto){
        return AjaxResult.success(merchantAppService.createQrCode(couponQrCodeDto));
    }

    /**
     * 优惠券二维码
     * @throws Exception
     */
    @GetMapping("/couponQrCode")
    public void couponQrCode(String orderNo, String amount, HttpServletResponse response) throws Exception {
        merchantAppService.couponQrCode(orderNo, amount, response);
    }

    /**
     * 效验二维码
     * @param tamp 订单号 + 生成二维码的时间戳 （MCD20251027171757254+1761724992）
     */
    @GetMapping("/verifyQrCode")
    public AjaxResult verifyQrCode(String tamp){
        return AjaxResult.success("", merchantAppService.verifyQrCode(tamp));
    }

    /**
     * 优惠券领取
     */
    @PostMapping("/couponCollection")
    public AjaxResult couponCollection(@RequestBody @Validated CouponCollectionDto couponCollectionDto){
        return AjaxResult.success(merchantAppService.couponCollection(couponCollectionDto));
    }

    /**
     * 优惠券发放
     */
    @PostMapping("/couponIssue")
    public AjaxResult couponIssue(@RequestBody @Validated CouponCollectionDto couponCollectionDto){
        return AjaxResult.success(merchantAppService.couponIssue(couponCollectionDto));
    }

}
